// pages/diary-details/diary-details.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // diaryId:"",//日记id
    diaryId: 1, //日记id
    pageSize: 10,
    pageNum: 1,
    content: '', //内容        
    replyId: "",
    arraydata: "", //下面赋的数据
    userdata: "", //下面赋的数据
    bookid: "", //日记本id
    imgUrl: "https://tyc-bj.cn/yc/api/attach/",
    pinglun: "", //评论数据
    time: '', //转的时间错
    kong: '', //发布后清空
    isshow: 0, //默认点赞
    options:'',//默认
    usid:wx.getStorageSync('userid'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log("q1q11w1w", options)
    that.setData({
      bookid: options.bookid,
      diaryId: options.diaryId,
      options:options,
    })

    //美丽日记列表上边内容
    wx.request({
      url: app.globalData.baseUrl + 'doctor/diaryBook/detail', //仅为示例，并非真实的接口地址
      data: {
        // bookId: that.data.bookId ,//日记本id
        bookId: that.data.bookid, //日记本id
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },
      success(res) {
        console.log('日记列表上边用户数据', res.data.data)
        that.setData({
          userdata: res.data.data,
          time: app.timeData(res.data.data.diaryBook.addTime),
        })
      }
    })


    this.details(); //日记详情
    this.obtain(); //获取日记评论
    this.browse(); //+浏览量
  },
  //美丽日记详情 
  details: function() {
    var that = this;
    //美丽日记详情
    wx.request({
      url: app.globalData.baseUrl + 'doctor/diary/detail', //仅为示例，并非真实的接口地址
      data: {
        diaryId: that.data.diaryId
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },
      success(res) {
        console.log('日记详情', res.data)
        that.setData({
          arraydata: res.data.data
        })
      }
    })
  },
  //获取日记评论
  obtain: function() {
    //获取日记评论
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/diary/commentPage', //仅为示例，并非真实的接口地址
      data: {
        diaryId: that.data.diaryId, //日记id
        pageSize: that.data.pageSize, //每页条数
        pageNum: that.data.pageNum, //页数
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,

        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },
      success(res) {
        console.log('获取日记评论123', res.data);
        that.setData({
          pinglun: res.data.data
        })
      }
    })
  },
  //浏览量增加
  browse: function() {
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/diary/addDiaryViews',
      data: {
        diaryId: that.data.diaryId,
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },
      success(res) {
        console.log('增加浏览数', res.data)
      }
    })
  },


  // 处理点赞按钮
  handlePoint: function(e) {
    var that = this;
    var isLogin = wx.getStorageSync('isLogin');
    if (!isLogin || isLogin == 2) {
      wx.showToast({
        title: '请登录!',
        icon: 'loading',
      })
      // 跳转到登入界面
      wx.reLaunch({
        url: '/pages/user-login/user-login'
      })
    } else {
      var that = this;
      console.log("sssss", e);
      if (e.currentTarget.dataset.isshow == 0 && e.currentTarget.dataset.id == that.data.diaryId) {
        console.log('传ID了么', e);
        // var id = e.target.dataset.exampleid;

        wx.request({
          url: app.globalData.baseUrl + 'doctor/diary/praise',
          data: {
            diaryId: that.data.diaryId,
          },
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          method: 'POST',
          success(res) {
            console.log('点赞结果', res);
            that.details();
            that.setData({
              id: that.data.diaryId,

            });
          }
        })
      }
    }
  },
  // 保存信息
  save: function(e) {
    var that = this
    console.log('点击:', e.detail.value);
    //内容
    if (e.target.dataset.name != that.data.content) {
      this.setData({
        content: e.detail.value
      });
      console.log("内容:", that.data.content)
    }
  },

  //日记发布
  fabu: function() {
    var that = this;
    //日记评论发布
    wx.request({
      url: app.globalData.baseUrl + 'doctor/diary/comment', //仅为示例，并非真实的接口地址
      data: {
        diaryId: that.data.diaryId,
        content: that.data.content,
        replyId: that.data.replyId

      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },
      success(res) {
        console.log('日记评论发布', res.data)
        that.setData({
          content: '',
        })
        that.obtain(); //获取日记评论
      }
    })
  },

  // 关注按钮
  bindFollow: function(e) {
    console.log('---dd--', e.currentTarget.dataset)
    var isLogin = wx.getStorageSync('isLogin');
    if (isLogin == 2) {
      wx.showModal({
        title: '提示',
        content: '请先登陆!',
        success: function(res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '../user-login/user-login',
            });
          } else {}
        }
      })
    } else {
      var userid = e.currentTarget.dataset.userid;
      var isfollow = e.currentTarget.dataset.isfollow;
      console.log('关注:userid', userid);
      var that = this;
      if (isfollow == 1) {
        wx.request({
          url: app.globalData.baseUrl + 'doctor/member/attention',
          data: {
            userId: userid,
          },
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          method: 'POST',
          success(res) {
            console.log('关注结果:', res)
            wx.showToast({
              title: '取消关注',
              icon: 'success',
              duration: 1000
            })
            that.onLoad(that.data.options);
          }
        })
      } else {
        wx.request({
          url: app.globalData.baseUrl + 'doctor/member/attention',
          data: {
            userId: userid,
          },
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          method: 'POST',
          success(res) {
            console.log('关注结果:', res)
            wx.showToast({
              title: '关注成功',
              icon: 'success',
              duration: 1000
            })
            that.onLoad(that.data.options);
          }
        })
      }

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})